#include <REGX52.H>
#include <intrins.h>

typedef unsigned int u16;	
typedef unsigned char u8;

#define SMG_A_DP_PORT	P0	


sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;


u8 gsmg_code[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
				0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void Delay1ms(u8 x)		
{
	while(x)
	{
	unsigned char i, j;

	_nop_();
	i = 2;
	j = 199;
	do
	{
		while (--j);
	} while (--i);
	x--;
  }
}

void smg_display(u8 i,u)
{


	   	switch(i)
		{
			case 1: LSC=1;LSB=1;LSA=1;break;
			case 2: LSC=1;LSB=1;LSA=0;break;
			case 3: LSC=1;LSB=0;LSA=1;break;
			case 4: LSC=1;LSB=0;LSA=0;break;
			case 5: LSC=0;LSB=1;LSA=1;break;
			case 6: LSC=0;LSB=1;LSA=0;break;
			case 7: LSC=0;LSB=0;LSA=1;break;
			case 8: LSC=0;LSB=0;LSA=0;break;
		}
		switch(u)
		{
			case 0:SMG_A_DP_PORT=gsmg_code[0];break;
			case 1:SMG_A_DP_PORT=gsmg_code[1];break;
			case 2:SMG_A_DP_PORT=gsmg_code[2];break;
			case 3:SMG_A_DP_PORT=gsmg_code[3];break;
			case 4:SMG_A_DP_PORT=gsmg_code[4];break;
			case 5:SMG_A_DP_PORT=gsmg_code[5];break;
			case 6:SMG_A_DP_PORT=gsmg_code[6];break;
			case 7:SMG_A_DP_PORT=gsmg_code[7];break;
		  case 8:SMG_A_DP_PORT=gsmg_code[8];break;
			case 9:SMG_A_DP_PORT=gsmg_code[9];break;
			case 'H':SMG_A_DP_PORT=0x76			 ;break;
			case 'C':SMG_A_DP_PORT=0x39      ;break;
		}
		
		
		Delay1ms(1);
		SMG_A_DP_PORT=0x00;
	}

void main()
{			u8 v=27;
		u8 s=50;
		u8 d=2;
		u8 c=1;
		while(1)
			{
				
			u8 a=0;
			for(a=1000;a>0;a--)
			{
			smg_display(1,v/10);
			smg_display(2,v%10);
			}

			if(P3_1==0)
				{
						Delay1ms(20);
						while (P3_1==0);
						Delay1ms(20);
						while(1)
						{
								u8 e=0;
								for(e=500;e>0;e--)
							{
								smg_display(1,v/10);
								smg_display(d,v%10);
							}
								d++;
								if(d>8)d=8;
							if(P3_0==0)
								{
									while(1)
									{
										u8 f=0;
										Delay1ms(20);
										while (P3_0==0);
										Delay1ms(20);
										
										for(f=500;f>0;f--)
										{
										smg_display(c,v/10);
										smg_display(d,v%10);
										}
										c++;
										if(c>7)c=7;
										if(P3_2==0)
									 {
											Delay1ms(20);
											while (P3_2==0);
											Delay1ms(20);
											while(1)
											{
											for(a=500;a>0;a--)
											{
												smg_display(c,v/10);
												smg_display(d,v%10);
											}
											v--;
											a=500;
											if(v==0)
												while(1)
												{
													smg_display(7,v/10);
													smg_display(8,v%10);
												}
											}
										}
											
											
									}
									
								}

				}
		 }
	}
}
		